'''
随机森林
'''

import scipy.io as scio
import datetime
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics

# 读取mat文件
def readMat(matPath):
    return scio.loadmat(matPath)

# 加载数据集
matPath='dataSet20180403-K100.mat'
dataSet=readMat(matPath)
print('数据集读取完成')

# 读取数据集
test=np.array(dataSet['test'])
testX=np.array(dataSet['testX'])
testY=np.array(dataSet['testY']).T[0]
train=np.array(dataSet['train'])
trainX=np.array(dataSet['trainX'])
trainY=np.array(dataSet['trainY']).T[0]
print('数据集读取完成')

trainXTemp=[]
trainYTemp=[]
for sampleIndex in range(len(trainX)):
    if trainY[sampleIndex]<0.5:
        if np.random.rand()<0.25:
            trainXTemp.append(trainX[sampleIndex])
            trainYTemp.append(trainY[sampleIndex])
    else:
        trainXTemp.append(trainX[sampleIndex])
        trainYTemp.append(trainY[sampleIndex])
trainX=trainXTemp
trainY=trainYTemp

# Boosting Tree准备
paraResult=[]
maxDepthArrary=[10,20,30,40,50,80,100]
nEstimatorsArrary=[5,10,20,50,100]
# maxDepthArrary=[100,200,500]
# nEstimatorsArrary=[100,200,500]
for maxDepth in maxDepthArrary:
    for nEstimators in nEstimatorsArrary:
        startTime = datetime.datetime.now()
        print('maxDepth:'+str(maxDepth)+',nEstimators:'+str(nEstimators))
        clf = RandomForestClassifier(max_depth=maxDepth,
                                 n_estimators=nEstimators)
        clf.fit(trainX, trainY)
        score=clf.score(trainX, trainY)
        print('训练精度：'+str(score))
        score=clf.score(testX,testY)
        print('测试精度：'+str(score))
        predictY=clf.predict(testX)
        print('召回率：'+str(metrics.recall_score(testY,predictY)))
        confusion_matrix = metrics.confusion_matrix(testY, predictY)
        print("混淆矩阵：\n %s" % confusion_matrix)
        TP = confusion_matrix[1][1]
        TN = confusion_matrix[0][0]
        FN = confusion_matrix[1][0]
        FP = confusion_matrix[0][1]
        accuracy = (TP + TN) / (TP + TN + FN + FP)
        Specitivity = TN / (TN + FP)
        Sensitivity = TP / (TP + FN)
        print("准确度： %s" % accuracy)
        print("特异度： %s" % Specitivity)
        print("敏感度： %s" % Sensitivity)

        paraResultItem={}
        paraResultItem['maxDepth']=maxDepth
        paraResultItem['nEstimators']=nEstimators
        paraResultItem['score']=score
        paraResult.append(paraResultItem)
        endTime = datetime.datetime.now()
        print(endTime - startTime)
print('RF结束')